package cn.hinglo.gameclaw.alipay.action;

import cn.hinglo.gameclaw.alipay.service.RechargeOrderService;
import cn.hinglo.gameclaw.data.annotation.IgnoreToken;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.CommonUtils;
import cn.hinglo.gameclaw.data.utils.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @createTime: 2018/5/16
 * @author: HingLo
 * @description: 客户端请求支付的订单信息
 */
@RestController
@RequestMapping("/pay/r/")
@Slf4j
public class RechargeOrderAction extends BaseController {

    @Autowired
    private RechargeOrderService rechargeOrderService;

    /***
     * 充值订单的下单信息
     * @param moneyId 充值类型的唯一Id
     * @return 下单的信息
     */
    @PostMapping("money")
    public Result moneyOrder(@RequestParam("moneyId") String moneyId, @RequestParam("type") Integer type) {
        String userId = getUserId(request);
        String orderInfo;
        // 通过充值订单的Id来查询充值信息
        log.info("用户充值下单：充值编号->{},用户->{},支付方式->{}", moneyId, userId, type);
        if (type == FieldUtils.PAYMENT_1) {
            //支付宝下单支付
            orderInfo = rechargeOrderService.moneyAlipayOrderService(moneyId, userId);

        } else {
            //表示微信下单支付
            orderInfo = rechargeOrderService.moneyWeixinOrderService(moneyId, userId, CommonUtils.getRemortIP(request));

        }
        if (orderInfo == null) {
            log.info("用户充值下单失败");
            return error("订单信息获取失败");
        } else {
            log.info("用户充值下单成功");
            return success(orderInfo);
        }
    }

    /**
     * 支付宝充值支付结果校验
     *
     * @return 是否处理成功
     */
    @PostMapping("moneyNotifyAlipay")
    @IgnoreToken
    public Result notifyAlipayMoneyOrder() {
        //验证是否支付失败
        return result(rechargeOrderService.moneyAlipayNotifyService(request), "处理失败");
    }

    /**
     * 微信充值支付结果通过
     *
     * @return 是否成功
     */
    @PostMapping("moneyNotifyWeixin")
    public Result notifyWeixinMoneyOrder() {
        //验证是否支付失败
        return result(rechargeOrderService.moneyWeixinNotifyService(request), "处理失败");
    }

}
